package cn.fuzongyao.learning.android;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class QuizActivity02 extends AppCompatActivity {

    private Button trueButton;
    private Button falseButton;
    private Button nextButton;
    private Button prevButton;
    private ImageButton nextImageButton;
    private ImageButton prevImageButton;
    private TextView questionTextView;
    private int currentIndex = 0;
    private Question[] questions = new Question[]{
            new Question(R.string.question_australia, true),
            new Question(R.string.question_oceans, true),
            new Question(R.string.question_mideast, true),
            new Question(R.string.question_africa, true),
            new Question(R.string.question_americas, true),
            new Question(R.string.question_asia, true)
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        questionTextView = findViewById(R.id.question_text_view);
        updateQuestion();
        questionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + 1) % questions.length;
                updateQuestion();
            }
        });

        trueButton = findViewById(R.id.true_button);
        trueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });

        falseButton = findViewById(R.id.false_button);
        falseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });

        View.OnClickListener nextClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + 1) % questions.length;
                updateQuestion();
            }
        };
        nextButton = findViewById(R.id.next_button);
        nextButton.setOnClickListener(nextClickListener);

        View.OnClickListener prevClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + questions.length - 1) % questions.length;
                updateQuestion();
            }
        };
        prevButton = findViewById(R.id.prev_button);
        prevButton.setOnClickListener(prevClickListener);

        nextImageButton = findViewById(R.id.next_image_button);
        nextImageButton.setOnClickListener(nextClickListener);
        prevImageButton = findViewById(R.id.prev_image_button);
        prevImageButton.setOnClickListener(prevClickListener);
    }

    private void updateQuestion() {
        questionTextView.setText(questions[currentIndex].getTextResId());
    }

    private void checkAnswer(boolean userPressedTrue) {
        boolean answerTrue = questions[currentIndex].isAnswerTrue();
        int messageResId = 0;
        if (answerTrue == userPressedTrue) {
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    }
}
